# -*- coding: utf-8 -*-
import pygame
import sys
def gcShow(gc):
    gcButtonNames = ("Tri","Circlr","Cross","Square", "LB", "RB", "LT", "RT", "Select","Start", "LPush", "RPush")
    tName = "Name : "+gc.get_name()
    tAxis = "Axes : "+str(gc.get_numaxes())
    tButton = "Buttons : "+str(gc.get_numbuttons())
    tPressed = ""
    for i in range(gc.get_numbuttons()):
        if (gc.get_button(i)):
            tPressed += gcButtonNames[i]
            tPressed += " "
    hori = int(gamer1.get_axis(0)*10)
    vert = int(gamer1.get_axis(1)*10)
    text1 = "(" + str(hori) + "," + str(vert) + ")"
    if hori < 0 and vert == 0:
        text2 = "Left"
    elif hori == 0 and vert > 0:
        text2 = "Down"
    elif hori > 0 and vert == 0:
        text2 = "Right"
    elif hori == 0 and vert < 0:
        text2 = "Up"
    elif hori < 0 and vert < 0:
        text2 = "Left+Up"
    elif hori < 0 and vert > 0:
        text2 = "Left+Down"
    elif hori > 0 and vert < 0:
        text2 = "Right+Up"
    elif hori > 0 and vert > 0:
        text2 = "Right+Down"
    else:
        text2 = "Center"
    print(text1)
    print(text2)
    print(tName)
    print(tAxis)
    print(tButton)

pygame.init()
gcCnt = pygame.joystick.get_count()
if gcCnt == 0:
    print("Error.....")
    sys.exit()
fpsClock = pygame.time.Clock()
screen = pygame.display.set_mode((800,600))
pygame.display.set_caption("Ex17.5")
gamer1 = pygame.joystick.Joystick(0)
gamer1.init()
background = pygame.Color( 0, 0, 0)

running = True
while running:
    fpsClock.tick(30)
    screen.fill(background)
    gcShow( gamer1 )
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.JOYBUTTONDOWN:
            gcShow(gamer1 )
        if event.type == pygame.JOYBUTTONUP:
            gcShow(gamer1 )
    pygame.display.flip()
pygame.quit()